package com.leyou.search.web;

import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.dto.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

/**
 * @author 虎哥
 */
@RestController
@RequestMapping("goods")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping("init")
    public ResponseEntity<String> initData(){
        // 初始化索引库
        searchService.createIndexAndMapping();
        // 加载数据
        searchService.loadData();
        // 返回
        return ResponseEntity.ok("索引库初始化完毕！");
    }

    /**
     * 获取自动补全的结果
     * @param key 用户输入的关键字
     * @return 根据关键字推荐的词条的集合
     */
    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key") String key){
        return searchService.getSuggestion(key);
    }

    /**
     * 搜索业务
     * @param paramDTO 搜索的请求参数
     * @return 搜索的分页结果
     */
    @PostMapping("list")
    public Mono<PageInfo<Goods>> searchGoods(@RequestBody SearchParamDTO paramDTO) {
        return searchService.searchGoods(paramDTO);
    }

    /**
     * 查询顶部过滤项
     * @param paramDTO 请求参数
     * @return 过滤项的集合
     */
    @PostMapping("/filter")
    public Mono<Map<String, List<?>>> getFilterList(@RequestBody SearchParamDTO paramDTO){
        return searchService.getFilterList(paramDTO);
    }
}
